package com.ruoyi.basic.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicSalary;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.BasicInsurance;
import com.ruoyi.basic.service.IBasicInsuranceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 保险基数Controller
 * 
 * @author ruoyi
 * @date 2023-01-17
 */
@Controller
@RequestMapping("/basic/insurance")
public class BasicInsuranceController extends BaseController
{
    private String prefix = "basic/insurance";

    @Autowired
    private IBasicInsuranceService basicInsuranceService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("basic:insurance:view")
    @GetMapping()
    public String insurance()
    {
        return prefix + "/insurance";
    }

    /**
     * 查询保险基数列表
     */
    @RequiresPermissions("basic:insurance:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicInsurance basicInsurance)
    {
        startPage();
        List<BasicInsurance> list = basicInsuranceService.selectBasicInsuranceList(basicInsurance);
        return getDataTable(list);
    }

    /**
     * 导出保险基数列表
     */
    @RequiresPermissions("basic:insurance:export")
    @Log(title = "保险基数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasicInsurance basicInsurance)
    {
        List<BasicInsurance> list = basicInsuranceService.selectBasicInsuranceList(basicInsurance);
        ExcelUtil<BasicInsurance> util = new ExcelUtil<BasicInsurance>(BasicInsurance.class);
        return util.exportExcel(list, "保险基数数据");
    }

    /**
     * 导入保险基数
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "员工保险基数", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:insurance:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicInsurance> util = new ExcelUtil<BasicInsurance>(BasicInsurance.class);
        List<BasicInsurance> insuranceList = util.importExcel(file.getInputStream());
        String message = basicInsuranceService.importInsurance(insuranceList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("basic:insurance:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicInsurance> util = new ExcelUtil<BasicInsurance>(BasicInsurance.class);
        return util.importTemplateExcel("员工保险基数信息");
    }

    /**
     * 新增保险基数
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        BasicUser user = basicUserService.selectBasicUserById(userId);
        mmap.put("user", user == null ? new BasicUser() : user);
        return prefix + "/add";
    }

    /**
     * 新增保存保险基数
     */
    @RequiresPermissions("basic:insurance:add")
    @Log(title = "保险基数", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicInsurance basicInsurance)
    {
        basicInsurance.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicInsuranceService.insertBasicInsurance(basicInsurance));
    }

    /**
     * 修改保险基数
     */
    @GetMapping("/edit/{insuranceId}")
    public String edit(@PathVariable("insuranceId") Long insuranceId, ModelMap mmap)
    {
        BasicInsurance basicInsurance = basicInsuranceService.selectBasicInsuranceByInsuranceId(insuranceId);
        mmap.put("basicInsurance", basicInsurance);
        return prefix + "/edit";
    }

    /**
     * 修改保存保险基数
     */
    @RequiresPermissions("basic:insurance:edit")
    @Log(title = "保险基数", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicInsurance basicInsurance)
    {
        basicInsurance.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicInsuranceService.updateBasicInsurance(basicInsurance));
    }

    /**
     * 删除保险基数
     */
    @RequiresPermissions("basic:insurance:remove")
    @Log(title = "保险基数", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basicInsuranceService.deleteBasicInsuranceByInsuranceIds(ids));
    }
}
